﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PromotionApp.Model;

namespace PromotionApp.UserControl
{
    public delegate void OnPagingChangedDelegate(object sender, PagingEventArgs e);

    public partial class ucPaging : System.Web.UI.UserControl
    {
        #region Property

        public int MaxPageNumber
        {
            get
            {
                return Convert.ToInt32(ViewState["MaxPageNumber"]);
            }
            set
            {
                ViewState["MaxPageNumber"] = value;
            }
        }

        public string SearchResult
        {
            set
            {
                this.lblSearchResult.Text = value;
            }
        }

        public string PageNumber
        {
            set
            {
                this.txtPageNumber.Text = value;
            }
        }

        public int PageSize
        {
            get
            {
                return int.Parse(this.ddlPageSize.SelectedValue);
            }
        }

        #endregion

        #region Event

        public event OnPagingChangedDelegate OnPagingChanged = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
                ImplementFirstPaging();
        }

        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            ImplementFirstPaging();
        }

        protected void btnSumitPaging_Click(object sender, EventArgs e)
        {
            try
            {
                int pageSize = int.Parse(this.ddlPageSize.SelectedValue);
                int pageNumber = int.Parse(this.txtPageNumber.Text.Trim());

                if (pageNumber <= 0)
                    ImplementFirstPaging();
                else if (pageNumber > this.MaxPageNumber)
                    ImplementLastPaging();
                else
                    ImplementPagingByPageNo(pageNumber);
            }
            catch (Exception ex)
            {
                ImplementFirstPaging();
            }
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            ImplementFirstPaging();
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            try
            {
                int pageSize = int.Parse(this.ddlPageSize.SelectedValue);
                int pageNumber = int.Parse(this.txtPageNumber.Text.Trim()) - 1;

                if (pageNumber >= 1)
                    ImplementPagingByPageNo(pageNumber);
            }
            catch (Exception ex)
            {
                ImplementFirstPaging();
            }
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            try
            {
                int pageSize = int.Parse(this.ddlPageSize.SelectedValue);
                int pageNumber = int.Parse(this.txtPageNumber.Text.Trim()) + 1;

                if (pageNumber <= this.MaxPageNumber)
                    ImplementPagingByPageNo(pageNumber);
            }
            catch (Exception ex)
            {
                ImplementFirstPaging();
            }
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            ImplementLastPaging();
        }

        #endregion

        #region Private Method

        private void ImplementPagingByPageNo(int pageNo)
        {
            int pageSize = int.Parse(this.ddlPageSize.SelectedValue);

            if (this.OnPagingChanged != null)
                this.OnPagingChanged(this, new PagingEventArgs(pageSize, pageNo));
        }

        private void ImplementLastPaging()
        {
            ImplementPagingByPageNo(this.MaxPageNumber);
        }

        private void ImplementFirstPaging()
        {
            ImplementPagingByPageNo(1);
        }

        #endregion
    }
}